package com.example.project3;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    // 定义消息代码
    private static final int LOGIN_SUCCESS = 1;
    private static final int LOGIN_FAILED = 0;
    private static final int LOGIN_START = 2;


    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;
    private ProgressBar progressBar;
    private CheckBox cbAgree;
    private TextView tvPasswordHint;

    // 创建Handler对象
    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOGIN_START:
                    progressBar.setVisibility(View.VISIBLE);
                    buttonLogin.setEnabled(false);
                    Toast.makeText(MainActivity.this, "登录中...", Toast.LENGTH_SHORT).show();
                    break;
                case LOGIN_SUCCESS:
                    progressBar.setVisibility(View.GONE);
                    buttonLogin.setEnabled(true);
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    break;
                case LOGIN_FAILED:
                    progressBar.setVisibility(View.GONE);
                    buttonLogin.setEnabled(true);
                    Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 修正布局文件引用
        setContentView(R.layout.activity_main);

        // 初始化控件（修正ID匹配问题）
        editTextUsername = findViewById(R.id.editText_username);
        editTextPassword = findViewById(R.id.editText_password);
        buttonLogin = findViewById(R.id.button_login);
        progressBar = findViewById(R.id.progressBar);
        cbAgree = findViewById(R.id.cbAgree);
        tvPasswordHint = findViewById(R.id.tvPasswordHint);

        // 监听复选框状态变化，控制登录按钮可用性
        cbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            buttonLogin.setEnabled(isChecked);
            buttonLogin.setBackgroundTintList(getResources().getColorStateList(
                    isChecked ? android.R.color.holo_blue_light : android.R.color.darker_gray));
        });

        // 设置登录按钮的点击监听器
        buttonLogin.setOnClickListener(v -> {
            String username = editTextUsername.getText().toString().trim();
            String password = editTextPassword.getText().toString().trim();

            // 简单验证
            if (username.isEmpty()) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.isEmpty()) {
                tvPasswordHint.setVisibility(View.VISIBLE);
                return;
            } else {
                tvPasswordHint.setVisibility(View.GONE);
            }

            // 启动子线程执行耗时操作
            new Thread(() -> {
                handler.sendEmptyMessage(LOGIN_START);

                try {
                    // 模拟网络请求
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 模拟验证逻辑
                if ("admin".equals(username) && "123456".equals(password)) {
                    handler.sendEmptyMessage(LOGIN_SUCCESS);
                } else {
                    handler.sendEmptyMessage(LOGIN_FAILED);
                }
            }).start();
        });
    }
}